package com.pomodorotechnique.Data;

import java.util.UUID;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;

import com.pomodorotechnique.Entity.Model_Task;
import com.pomodorotechnique.toolkit.Tool;
import com.sun.swing.internal.plaf.basic.resources.basic;

public class Task_SqlGS extends Model_gs_Sql<Model_Task, UUID> implements ITask_GS {

	public Task_SqlGS(Context context) {
		super(context, "task", new int[]{0});
		// TODO Auto-generated constructor stub
	}

	@Override
	public void initColumnNames() {
		// TODO Auto-generated method stub
		columnNameStrings = new String[]{
			"uuid","title","descrpition","isUnexpected","deadline","arrangedtimeFrom",
			"arrangedtimeTo","recordDatetime","status","type","titlefirstletter"	
		};
	}

	@Override
	public void setAttsMap() {
		// TODO Auto-generated method stub
		atts = new Object[columnNameStrings.length];
		atts[0] = model.uuid;
		atts[1] = model.title;
		atts[2] = model.description;
		atts[3] = model.isUnexpected;
		atts[4] = model.deadline;
		atts[5] = model.arrangedTimeFrom;
		atts[6] = model.arrangedTimeTo;
		atts[7] = model.recrodeDateTime;
		atts[8] = model.status;
		atts[9] = model.type;
		atts[10] = model.titlefirstletter;
	}

	@Override
	public void Set(String name, String value) {
		// TODO Auto-generated method stub
		int i = Find(name);
		model = new Model_Task();
		switch (i) {
		case 0:
			model.uuid = Tool.toUuid(value);
		case 1:
			model.title = value;
			break;
		case 2:
			model.description = value;
			break;
		case 3:
			model.isUnexpected = Boolean.parseBoolean(value);
			break;
		case 4:
			model.deadline = Tool.toDate(value);
			break;
		case 5:
			model.arrangedTimeFrom = Tool.toDate(value);
			break;
		case 6:
			model.arrangedTimeTo = Tool.toDate(value);
			break;
		case 7:
			model.recrodeDateTime = Tool.toDate(value);
			break;
		case 8:
			model.status = Integer.parseInt(value);
			break;
		case 9:
			model.type = Integer.parseInt(value);
			break;
		case 10:
			model.titlefirstletter = value;
		default:
			break;
		}
	}
	
	public Cursor getTotalRes(UUID uuid){
		//String sql = "select sum(count) as 'total',task_uuid,title from allocation inner join task on task.uuid = task_uuid group by task_uuid";
		StringBuffer sb = new StringBuffer("select task.uuid,title,ifnull(sum(count),0) as 'total',titlefirstletter from task left outer join allocation on task.uuid = task_uuid group by task.uuid");
		String args[] = null;
		if(null != uuid){
			sb.append(" where task_uuid = ?");
			args = new String[]{uuid.toString()};
		}
		sb.append(" order by titlefirstletter,title");
		return this.rawQuery(sb.toString(),args);
	}

}
